<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  website
 */

class m_website_actions_block_front_FreeContentEditAction extends m_backoffice_extends_BlockEditModal
{
	public function execute()
	{
		$model = $this->getBlockContext()->getParameters();	

		$documentId = $model["pageId"];
		$documentLang = $model["blockLang"];
		$blockId = $model["blockId"];

		$tempDocService = m_backoffice_services_TempDocService::getInstance();
		
        $page = $this->getPage();
		
		if(isset($model['contentId']) && !is_null($model['contentId']) && $model['contentId'] != "")
		{
			if($tempDocService->hasTemporary($model['contentId'], $documentLang))
			{
				$doc = $tempDocService->getTemporaryDocument($model['contentId'], $documentLang);	
			}
			else
			{
				if(!is_null($model['revision']) && $model['revision'] != "")
				{
					$doc = f_document_Provider::getInstance()->getByModelAndUniqueId('website/FreeContent', $model['contentId'], $documentLang);

                    if(intval($model['revision']) != $doc->getLastRevisionNumber())
                    {
                        $doc = $doc->getRevision(intval($model['revision']));
                    }

				}
				else
				{
					$doc = f_document_Provider::getInstance()->getByModelAndUniqueId('website/FreeContent', $model['contentId'], $documentLang);
				}				
			}
			
			$model['content'] = m_website_services_FreeContentService::getInstance()->parseContent($doc->getContent(), $doc->getLang());
		}
		
		$block = $page->getBlockById($blockId);
		
		foreach($model as $key => $value)
		{
			if($key != "pageId" && $key != "blockId" && $key != "content")
			{
				$block[$key] = stripslashes($value);
			}
		}

		$page->setBlockParamsById($blockId, $block);

        $model['website'] = $page->getWebsite();

		$this->setModel($model);		
		$this->setView('success');
	}
}